import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
// import remove from '@dfeidao/atom-web/local/remove';

import get from '@dfeidao/atom-web/local/get';
// import set from '@dfeidao/atom-web/local/set';


import javadsd from '@dfeidao/atom-web/msg/javadsd';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import a002 from './a002';

export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// 保存
	// if (get(fd, 'prevent')) {
	// 	return;
	// }
	// set(fd, 'prevent', 1);


	const tenant_name = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-name="name"]').value;
	if (!tenant_name) {
		error('租户名称不能为空!');
		return;
	}
	const link_man = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-name="user"]').value;
	const phone = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-name="phone"]').value;
	const email = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-name="email"]').value;

	const tenant_level = fd.data.node.querySelector<HTMLSelectElement>('[data-feidao-name="levl"]').value;
	if (tenant_level === '请选择') {
		error('等级不能为空!');
		return;
	}
	const productid = get(fd, 'productid');

	const res = await nodejs<{ status: string, tenantid: string }>('tenant-inf/zj-000002/s002', { productid, tenant_name, link_man, phone, email, tenant_level });
	// debugger;
	if (res.status === 'success') {
		const tenantid = res.tenantid;
		const condition = {
			param: {
				tenantid,
				productid
			}
		};

		try {
			const dsd = await javadsd<{ code: number }>('12', '', 'tenantinfoinit', condition);
			if (dsd.code === 1) {
				success('保存成功!');
				// remove(fd, 'prevent');
				await a002(fd);
			} else {
				const res1 = await nodejs('tenant-inf/zj-000002/s003', { productid, tenantid });
				if (res1 === 'success') {
					error('保存成功!');
					return;
				} else {
					error('保存失败!');
					return;
				}
			}
		} catch (err) {
			const res1 = await nodejs('tenant-inf/zj-000002/s003', { productid, tenantid });
			if (res1 === 'success') {
				error('保存失败!');
				return;
			} else {
				error('保存失败!');
				return;
			}
		}


	} else if (res.status === 'fail') {
		error('保存失败!');
		return;
	} else {
		error('租户名称重复!');
		return;
	}

}
